import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/components/network_image_load/image_utils.dart';
import 'package:yt_devops/components/network_image_load/load_image.dart';
import 'package:yt_devops/components/yt_extension.dart';
import 'package:yt_devops/pages/mine/mine/mine_controller.dart';
import 'package:yt_devops/resources/resources.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/routers/router_path.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/user_entity_manager.dart';
import 'package:yt_devops/utils/constants/permission_page_constant.dart';
import 'package:yt_devops/utils/enums.dart';

class MinePage extends GetView<MineController> {
  MinePage({Key? key}) : super(key: key);
  @override
  final controller = Get.put(MineController());

  @override
  Widget build(BuildContext context) {
    Log.d('MinePage==');
    return GetBuilder<MineController>(
      builder: (_) {
        return Scaffold(
          backgroundColor: Colour.backgroundColor,
          body: Column(
            children: [
              avatarContainer(),
              Container(
                margin: EdgeInsets.only(left: 16.w, right: 16.w, top: 16),
                // padding: EdgeInsets.only(left: 16.w,top: 16.w),
                clipBehavior: Clip.hardEdge,
                decoration: BoxDecoration(
                    color: Colors.white,
                    boxShadow: [
                      BoxShadow(
                        color: const Color(0xff546977).withOpacity(0.05),
                        // 设置阴影颜色
                        blurRadius: 6,
                        // 阴影模糊程度
                        offset: const Offset(0, 3), // 阴影偏移量
                      ),
                    ],
                    borderRadius: const BorderRadius.all(Radius.circular(8))),
                height: 56.0 * (controller.topItems.length),
                child: ListView.builder(
                  padding: EdgeInsets.zero,
                  physics: const NeverScrollableScrollPhysics(),
                  itemBuilder: (context, index) {
                    return _itemWidget(controller.topItems[index],
                        isLast: index == controller.topItems.length - 1);
                  },
                  itemCount: controller.topItems.length,
                ),
              ),
              Container(
                margin: EdgeInsets.only(left: 16.w, right: 16.w, top: 16),
                // padding: EdgeInsets.only(left: 16.w,top: 16.w),
                clipBehavior: Clip.hardEdge,
                decoration: BoxDecoration(
                    color: Colors.white,
                    boxShadow: [
                      BoxShadow(
                        color: const Color(0xff546977).withOpacity(0.05),
                        // 设置阴影颜色
                        blurRadius: 6,
                        // 阴影模糊程度
                        offset: const Offset(0, 3), // 阴影偏移量
                      ),
                    ],
                    borderRadius: const BorderRadius.all(Radius.circular(8))),
                height: 56.0 * controller.bottomItems.length,
                child: ListView.builder(
                  padding: EdgeInsets.zero,
                  physics: const NeverScrollableScrollPhysics(),
                  itemBuilder: (context, index) {
                    return _itemWidget(controller.bottomItems[index],
                        isLast: index == controller.bottomItems.length - 1);
                  },
                  itemCount: controller.bottomItems.length,
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  Container avatarContainer() {
    return Container(
      height: 206 + ScreenUtil().statusBarHeight + 40,
      width: double.infinity,
      decoration: BoxDecoration(
        // color: Colors.red,
        image: DecorationImage(
          fit: BoxFit.fill,
          image: ImageUtils.getImageProvider(
              UserEntityManager().systemInfo.appPerson!),
        ),
      ),
      child: Stack(
        children: [
          Container(
              height: 206 + ScreenUtil().statusBarHeight,
              width: double.infinity,
              decoration: BoxDecoration(
                image: DecorationImage(
                  fit: BoxFit.fill,
                  image: ImageUtils.getImageProvider(
                      UserEntityManager().systemInfo.appPerson!),
                ),
              )),
          Container(
              margin: EdgeInsets.only(
                  top: ScreenUtil().statusBarHeight + 40 + 88,
                  left: 16.w,
                  right: 16.w),
              height: 142,
              decoration: BoxDecoration(
                color: Colour.white,
                borderRadius: const BorderRadius.all(Radius.circular(8)),
                boxShadow: [
                  BoxShadow(
                    color: Colors.black.withOpacity(0.08), // 设置阴影颜色
                    blurRadius: 12, // 阴影模糊程度
                    offset: const Offset(0, 2), // 阴影偏移量
                  ),
                ],
              ),
              width: ScreenUtil().screenWidth - 32.w,
              child: GestureDetector(
                onTap: () {
                  controller.gotoUserInfo();
                },
                child: Column(
                  children: [
                    Container(
                      margin: const EdgeInsets.only(top: 50),
                      child: Text(
                        controller.state.nickName ?? "",
                        style: const TextStyle(
                            color: Colour.mainTextColor,
                            fontSize: 24,
                            fontWeight: FontWeight.bold),
                        maxLines: 2,
                        overflow: TextOverflow.ellipsis,
                      ),
                    ),
                    Text(
                      StringJudgment.hideMiddleFourDigits(
                        controller.state.phone ?? '',
                      ),
                      style: const TextStyle(
                        color: Colour.lightTextColor,
                        fontSize: 14,
                      ),
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                    ),
                  ],
                ),
              )),
          Positioned(
            top: 80 + ScreenUtil().statusBarHeight,
            left: (ScreenUtil().screenWidth - 94) * 0.5,
            child: GestureDetector(
              onTap: () {
                controller.gotoUserInfo();
              },
              child: Container(
                margin: const EdgeInsets.only(top: 0),
                height: 94,
                width: 94,
                clipBehavior: Clip.hardEdge,
                decoration: BoxDecoration(
                    border: Border.all(width: 2.5, color: Colour.white),
                    borderRadius: const BorderRadius.all(Radius.circular(47))),
                child: ClipOval(
                  child: LoadImage(
                    controller.state.avatar ?? '',
                    holderImg: 'assets/images/mine_avatar',
                    width: 94,
                    height: 94,
                  ),
                ),
              ),
            ),
          )
        ],
      ),
    );
  }

  _itemWidget(Map map, {bool isLast = false}) {
    return Container(
        color: Colors.white,
        height: 56,
        child: Column(
          children: [
            SizedBox(
              height: 55,
              child: ListTile(
                minLeadingWidth: 0,
                leading: Image.asset(map['icon']),
                title: Text(
                  map['title'].toString().tr,
                  style: const TextStyle(
                    color: Colour.mainTextColor,
                  ),
                ),
                trailing: Image.asset(ImageAssets.rightArrow),
                onTap: map['onTap'],
              ),
            ),
            Visibility(
                visible: !isLast,
                child: Container(
                    margin: EdgeInsets.only(left: 16.w, right: 16.w),
                    height: 0.5,
                    color: Colour.dividerColor)),
          ],
        ));
  }
}
